package cn.lg.soar.system.biz.modules.monitor.client;

import cn.lg.soar.core.annotations.ApiClient;
import cn.lg.soar.mvc.log.ILogConfigService;
import cn.lg.soar.mvc.log.LogConfigDTO;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.enums.LogConfigStatusEnum;
import cn.lg.soar.system.biz.config.SystemCacheKey;
import cn.lg.soar.system.biz.modules.general.entity.LogConfig;
import cn.lg.soar.system.biz.modules.general.service.LogConfigService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 日志配置接口
 * @author luguoxiang
 * @date 2021-08-11
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Hidden
@Primary
@ApiClient("boot")
@RestController
@RequestMapping(ModuleConst.CLIENT_PATH + "/log-config")
public class LogConfigApiClient implements ILogConfigService {

    @Autowired
    private LogConfigService configService;

    @Cacheable(value = SystemCacheKey.LOG_CONFIG, key = "#p0", cacheManager = "remote")
    @PostMapping("getLogConfig")
    @Override
    public LogConfigDTO getLogConfig(@RequestParam("code") String code) {
        LogConfig source = configService.getOne(
                Wrappers.<LogConfig>lambdaQuery()
                        .eq(LogConfig::getPath, code)
                        .eq(LogConfig::getStatus, LogConfigStatusEnum.normal)
        );

        if (source == null) {
            return null;
        }

        LogConfigDTO target = new LogConfigDTO();

        target.setType(source.getType());
        target.setName(source.getName());
        target.setContent(source.getContent());
        target.setArgsMode(source.getArgsMode());
        target.setArgsEl(source.getArgsEl());
        target.setArgsIndex(source.getArgsIndex());
        target.setResultMode(source.getResultMode());
        target.setResultEl(source.getResultEl());
        target.setErrorMode(source.getErrorMode());

        return target;
    }

}
